<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>

<script type="text/javascript">
	var org_treegrid;
	$(function() {
		org_treegrid = $('#org_treegrid').treegrid( {
							url : '${ctx}/sys/org/treegrid',
							idField : 'id',
							treeField : 'text',
							parentField : 'pid',
							rownumbers : true,
							pagination : false,
						
							sortName : 'sort',
							sortOrder : 'asc',
							frozenColumns : [ [ {
								width : '120',
								title : '机构名称',
								field : 'text'
							} ] ],
							columns : [ [	
									{
										width : '60',
										title : '类型',
										field : 'type',
										formatter : function(value, row) {
											if (!value) {
												return sy.formatString("<span align='center'>机构</span>");
											} else {
												return sy.formatString("<span>部门</span>");
											}
										}
									},
									{
										width:"60",
										title:"状态",
										field:"state",
										formatter:function(value,row){
											if(!value){
												return sy.formatString("<span>未启用</span>");
											}else{
												return sy.formatString("<span>启用</span>");
											}
										}
									},
									{
										width : '200',
										title : '机构描述',
										field : 'remarks'
									},
									{
										width:'60',
										title:'创建人',
										field:'createBy'
									},
									{
										width:'100',
										title:'创建时间',
										field:'createDate'
										
									},
									{
										title : '操作',
										field : 'action',
										width : '150',
										formatter : function(value, row) {
											var str = "";
												<shiro:hasPermission name="/sys/res/update/pre">
													str +=sy.formatString('<span onclick="org_editFun(\'{0}\');" style="margin-left:3px;cursor:pointer;">【{1}】</span>',row.id,"修改");
												</shiro:hasPermission>
												<shiro:hasPermission name="/sys/res/delete"> 
													str +=sy.formatString('<span onclick="org_removeFun(\'{0}\');" style="margin-left:3px;cursor:pointer;">【{1}】</span>',row.id,"删除");
												</shiro:hasPermission>
											return str; 
										}
									} ] ],
							toolbar : '#org_toolbar',
							onBeforeLoad : function(row, param) {
								parent.$.messager.progress({
									text : '数据加载中....'
								});
							},
							onLoadSuccess : function(row, data) {
								parent.$.messager.progress('close');
							}
						});
	});
	//添加
	
	var org_addFun = function() {
		$('<div/>').dialog({
			href : '${ctx}/sys/org/update/json',
			width : 640,
			height : 320,
			modal : true,
			title : '添加机构',
			buttons : [ {
				text : '添加',
				iconCls : 'icon-add',
				handler : function() {
					var d = $(this).closest('.window-body');
					$('#org_updateForm').form('submit', {
						url : '${ctx}/sys/org/update/json/pre',
						success : function(result) {
							try {
								var r = $.parseJSON(result);
								if (r.success) {
									org_treegrid.treegrid('reload'); // 刷新数据  
									d.dialog('destroy');
								}
								$.messager.show({
									title : '温馨提示',
									msg : r.message
								});

							} catch (e) {
								$.messager.alert('温馨提示', r.message);
								return;
							}

						}
					});
				}

			} ],
			onClose : function() {
				$(this).dialog('destroy');
			}
		});

	};

	//查看
	/*var res_lookFun = function(id) {

	};*/

	//修改
	var org_editFun = function(id) {
		$('<div/>').dialog({
			href : '${ctx}/sys/org/update/json',
			width : 640,
			height : 320,
			modal : true,
			title : '修改机构',
			buttons : [ {
				text : '修改',
				iconCls : 'icon-add',
				handler : function() {
					var d = $(this).closest('.window-body');
					$('#org_updateForm').form('submit', {
						url : '${ctx}/sys/org/update/json/pre',
						success : function(result) {
							try {
								var r = $.parseJSON(result);
								if (r.success) {
									org_treegrid.treegrid('reload'); // 刷新数据  
									d.dialog('destroy');
								}
								$.messager.show({
									title : '温馨提示',
									msg : r.message
								});

							} catch (e) {
								$.messager.alert('温馨提示', r.message);
								return;
							}

						}

					});
				}

			} ],
			onClose : function() {
				$(this).dialog('destroy');
			},
			onLoad : function() {
				$.post('${ctx}/sys/org/update/json/pre/sel',{id:id}, function(result) {
					if(result!=null){			
						$('#org_updateForm').form('load', result);
					}
					$.messager.progress('close');
				}, 'json'); 
			}
		});
	};

	//移除
	var org_removeFun = function(id) {
		parent.$.messager.confirm('询问', '您确定要删除此记录？', function(r) {
			if (r) {
				$.post('${ctx}/sys/org/update/del?id=' + id, {
					id : id
				}, function(data) {
					org_treegrid.treegrid('reload');
					$.messager.alert('温馨提示', data.message);
				}, 'json');
			}
		});
	};

	//展开
	var org_redoFun = function() {
		var node = org_treegrid.treegrid('getSelected');
		if (node) {
			org_treegrid.treegrid('expandAll', node.id);
		} else {
			org_treegrid.treegrid('expandAll');
		}
	};
	//折叠
	var org_undoFun = function() {
		var node = org_treegrid.treegrid('getSelected');
		if (node) {
			org_treegrid.treegrid('collapseAll', node.id);
		} else {
			org_treegrid.treegrid('collapseAll');
		}
	};
</script>



<!-- 界面布局  -->
<div class="easyui-layout" style="width: 100%; height: 100%;"
	data-options="fit:true,border:false">
	<div id="org_toolbar" style="display: none;">
		<table>
			<tr>
				<td><a href="javascript:void(0);" class="easyui-linkbutton"
					data-options="iconCls:'icon-add',plain:true"
					onclick="org_addFun();">添加</a></td>
				<td><div class="datagrid-btn-separator"></div></td>
				<td><a onclick="org_redoFun();" href="javascript:void(0);"
					class="easyui-linkbutton"
					data-options="plain:true,iconCls:'icon-redo'">展开</a> <a
					onclick="org_undoFun();" href="javascript:void(0);"
					class="easyui-linkbutton"
					data-options="plain:true,iconCls:'icon-undo'">折叠</a></td>
				<td><div class="datagrid-btn-separator"></div></td>
				<td><a onclick="org_treegrid.treegrid('reload');"
					href="javascript:void(0);" class="easyui-linkbutton"
					data-options="plain:true,iconCls:'icon-reload'">刷新</a></td>
			</tr>
		</table>
	</div>
	<div data-options="region:'center',title:'机构信息'" style="padding: 2px;">
		<div class="easyui-layout" data-options="fit : true,border : false">
			<div data-options="region:'center',border:false">
				<table id="org_treegrid"></table>
			</div>
		</div>
	</div>
</div>